﻿using MesGLApp.BLL.Mes;
using MesGLApp.DAL.MesDB;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MesGLApp.View.Search
{
    partial class SearchExcepOrderFrm : Smobiler.Core.Controls.MobileForm
    {
        #region "内部变量"
        private int pageIndex = 0;
        private ExcepOrderControler _ctrl = new ExcepOrderControler();
        private List<ExcepOrder> _repair_list = new List<ExcepOrder>();

        private List<StatusInfo> _status_list = new List<StatusInfo>();
        private StatusInfo _curr_status = null;
        #endregion

        public SearchExcepOrderFrm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }

        #region "控件事件"
        /// <summary>
        /// 读入页面时，加载所有的异常状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchExcepOrderFrm_Load(object sender, EventArgs e)
        {
            List<StatusInfo> tmpList = this._ctrl.SearchAllStatus();
            if (tmpList != null && tmpList.Count > 0)
            {
                var query = tmpList.Where(s => s.StypeID == 4)
                                    .Where(s => s.StatusValue != "暂停" && s.StatusValue != "取消暂停" && s.StatusValue != "无异常")
                                    .OrderBy(s => s.StatusCode);
                if (query != null && query.Count() > 0)
                {
                    this._status_list.AddRange(query);
                }
            }

            this.SetPopStatusDataBinding(this._status_list);
        }

        /// <summary>
        /// 打开异常选择列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStatus_Press(object sender, EventArgs e)
        {
            if (this._status_list != null && this._status_list.Count > 0)
            {
                popErrStatus.ShowDialog();
            }
            else
            {
                MessageBox.Show("没有任何可以选择的异常状态！", "提示");
            }
        }

        /// <summary>
        /// 选中需要查询的异常状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void popErrStatus_Selected(object sender, EventArgs e)
        {
            this.btnStatus.Text = this.popErrStatus.Selection.Text.Trim();
            if (this._status_list != null && this._status_list.Count > 0)
            {
                var query = this._status_list.Where(s => s.StatusValue == this.popErrStatus.Selection.Text.Trim());
                if (query != null && query.Count() > 0)
                {
                    this._curr_status = query.First<StatusInfo>();
                }
            }
        }

        /// <summary>
        /// 打开维修状态选择列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRepairStatus_Press(object sender, EventArgs e)
        {
            this.popRepairStatus.ShowDialog();
        }

        /// <summary>
        /// 选择维修状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void popRepairStatus_Selected(object sender, EventArgs e)
        {
            this.btnRepairStatus.Text = this.popRepairStatus.Selection.Text.Trim();
        }

        /// <summary>
        /// 扫码工单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScan_BarcodeScanned(object sender, BarcodeResultArgs e)
        {
            this.txtProdNumber.Text = e.Value.ToString();
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 初始化异常状态
        /// </summary>
        /// <param name="statusList"></param>
        private void SetPopStatusDataBinding(List<StatusInfo> statusList)
        {
            this.popErrStatus.Groups.Clear();
            if (statusList != null && statusList.Count > 0)
            {
                PopListGroup tmpGroup = new PopListGroup();
                foreach (StatusInfo status in statusList)
                {
                    tmpGroup.AddListItem(status.StatusValue, status.ID.ToString());
                }
                this.popErrStatus.Groups.Add(tmpGroup);
            }
        }








        #endregion

        private void btnSearch_Press(object sender, EventArgs e)
        {

        }
    }
}